
	***data paths 
  
   global posted /Volumes/Sans titre/DATA_MICROEU/Posting
   global data /Users/Mathilde/Dropbox/Posted_workers/Data
   
   
   
   **output paths
  global replication_outputs_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_figures
  global replication_estimates /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_estimates
  global replication_estimates_a /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Appendix_estimates
  global replication_outputs /Users/Mathilde/Dropbox/Posted_workers/Replication/TNT_final/Replication_package/Main_figures

  global frontalier 1 2 4 5 6 8 9 25 31 39 54 55 57 59 64 65 66 67 68 71 74 90


	
	
	**************************************************************************************************
	*. Appendix D3: Remaining supplementary tests (most are already produced in Fig5_Tab2.do)
	**************************************************************************************************
	
    *alternative inference for standard errors 
	
	use "$data/FD_Figure5.dta", clear
	cap drop _m
	merge 1:1 code_insee using "$data/initialexposure.dta", keepusing(s_reg)
	drop _m 
	ivreg_ss deltadom20032015, endogenous_var(logexposure_post_pp) shiftshare_iv(logexposure_pre_pp) share_varlist(s_reg) weight_var(pop2003)

    use "$data/FD_Figure5.dta", clear
	cap drop _m
	merge 1:1 code_insee using "$data/initialexposure.dta", keepusing(s_reg)
	drop _m 
	*Table D20 
	ivreg_ss deltadom20032015, endogenous_var(logexposure_post_pp) shiftshare_iv(logexposure_pre_pp) share_varlist(s_postable) weight_var(pop2003)
	
	
	*alternative normalization for IV 
	
	use "$data/FD_Figure5.dta", clear
	gen logexposure_2000=log(posting_pre/totemp2000)
	*Table D20 	
	ivreg2 deltadom20032015 (logexposure_post_pp=logexposure_2000) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first
	ivreg2 deltadom20032015 (exposure_post_pp=logexposure_2000) s_postable2003 s_ind2003 s_ouvrier [aweight=pop2003], ro first


	*******************************************************************
	*Creation of province-level exposure to imports from NMS countries 
    *Loading raw data on imports of goods  
	******************************************************************
	
import delimited "$data/stat-national-ce-import/NATIONAL_NC8PAYSI.txt", clear 
collapse (sum) v9, by(v7 v5)
g NMS=0
global varlist PL EE SI SK LT LV RO BG CZ
foreach y in $varlist{
replace NMS=1 if v7=="`y'"
}
drop if NMS==0
collapse (sum) v9, by(v5)
egen tot=sum(v9)
g s=v9/tot
save  "$data/nmsimports.dta", replace 


import delimited "$data/stat-national-ce-import/Libelle_A129.txt", clear 
keep v1 v2
rename v1 v5
save  "$data/codeimports.dta", replace 

*this datafile has code correspondance between trade codes and employment codes 
   import excel "$data/correspondance_imports.xlsx", sheet("Sheet1") firstrow clear
   keep v5 sect 
   save "$data/correspondance_imports.dta",replace


use "$data/nmsimports.dta", clear 
merge 1:1 v5 using "$data/codeimports.dta"
drop _m
merge 1:1 v5 using "$data/correspondance_imports.dta"
keep if _m==3
save "$data/correspondance.dta", replace 


use "$data/correspondance.dta", clear
rename v9 value
collapse (sum) value, by(sect)
   egen tot=sum(value)
   g s_nms=value/tot
   drop tot
   save  "$data/NMSimportshock.dta", replace
  
  *import data on french employment by province for sectors where imports are matched 
 use "$data/Employment_France/INSEEACOSS/HTAZ.dta", clear
foreach y in  TBEC1 TBEC2 TBEC3 TBEC4 TBEC5 TBEC5CB  TBEC5CC TBEC5CE TBEC5CF TBEC5CG TBEC5CH TBEC3CI TBEC3CK {
	append using "$data/Employment_France/INSEEACOSS/H`y'.dta"	
}
keep if year==2003
destring n, replace
rename n employment
rename sector sect
merge m:1 sect using "$data/NMSimportshock.dta"

egen tot=sum(employment), by(sect)
g s_province=employment/tot
g shock=s_province*value 
collapse (sum) shock, by(code_insee)
save "$data/tradeshock.dta", replace


********************************************
* Exposure to NMS2007 shock 
*******************************************


 
	*Raw data on posting forms, with information on origin of posted workers by province
	import excel "$data/DPD_declarations_forstata.xls", sheet("salaries") firstrow clear
	rename Departement code_insee
	rename Annee year
	keep if year<2016
    replace code_insee=code_insee/10 if code_insee>9 
	collapse (sum) nbsal nms1 nms2 eu15, by(code_insee year)
	egen tot=sum(nms2), by(year)
	g s2=nms2/tot
	g s=nms2/nbsal
	save "$data/nms2.dta", replace
	
	use "$data/panel_Figure5.dta", clear 
	keep if postable==1
	cap drop _m
	merge 1:1 code_insee year using "$data/nms2.dta"
    drop if _m==2
	replace nms2=0 if missing(nms2)
	g exposure_nms2=nms2/totemp2003
	egen exposure_nms2_pred=mean(exposure_nms2) if year<2007, by(code_insee)
	egen exposure_nms2_post=mean(exposure_nms2) if year>2006, by(code_insee)
	egen expnms2_pre=min(exposure_nms2_pred), by(code_insee)
	egen expnms2_post=min(exposure_nms2_post), by(code_insee)
	
	 g logprenms2=log(expnms2_pre)
	 g logpostnms2=log(expnms2_post)
	 
	 reg logprenms2 logpostnms2, ro 
	 global beta=round(_b[logpostnms2],.001)
	 global se=round(_se[logpostnms2],.01)
 
	 binscatter logprenms2 logpostnms2 if year==2005, nq(40) ///
	 graphregion(fcolor(white) lcolor(white)) xtitle("Exposure to Postings from NMS 2007 After 2007") ///
	 ytitle("Exposure to Postings from NMS 2007 Before 2007") ///
	 ttext( -15 -6 "Corr=$beta($se)", place(e))
   	 graph export "$replication_estimates_a/firststage_nms2007.pdf", replace
 
	 binscatter expnms2_pre expnms2_post if year==2005, nq(40) 
 
     *exposure rank to NMS2007 shock 
     egen rank_nms2=xtile(expnms2_pre), by(year) nq(10)
   
     collapse (sum) employment pop, by(year rank_nms2)
   
    g emprel=emp/pop
    *normalize to 2003 to not create artificial "gap" in 2007 by changing normalization
	*compared to baseline 
	
    foreach y in employment {
	g normp`y'=`y' if year==2003
	egen norm`y'=min(normp`y'), by(rank_nms2)
	g normemp`y'=`y'/norm`y'
	}
	
	drop if year<1993


   g treated=.
   replace treated=1 if rank_nms2==10
   replace treated=0 if rank_nms2<5

    g post=0
    replace post=1 if year>2006 
    g logemp=log(employment)
    reg logemp i.treated i.post i.treated##i.post i.year,ro
    global beta=round(_b[1.treated#1.post],.001)
	global se=round(_se[1.treated#1.post],.01)
	twoway (connected normempemployment year if treated==1 & year>1993, lcolor(cranberry) mcolor(cranberry)) ///
	(connected normempemployment year if treated==0  & year>1993, lcolor(dknavy) mcolor(dknavy)), ///
	legend(label(1 "Top 10% Exposure") label(2 "Bottom 40% Exposure") cols(1) ring(0) pos(11)) ylabel(0.7 (0.3) 1.2) ///
	graphregion(fcolor(white) lcolor(white)) ///
	xlabel(1994 (1) 2015, angle(vertical)) xtitle("") ytitle("Employment in Postable Sectors (2003=1)") ///
	xline(2006, lcolor(red)) xline(2004, lcolor(red) lpattern(dash)) ///
	ttext(1.2 2010 "DiD: $beta($se)", place(e))
   
   
   
   graph export "$replication_outputs_a/FigureD48.pdf", replace

